//system libs
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
//c++ libs
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

const int BACK_LOG = 20;

int main()
{
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;
    int result;
    fd_set readfds, testfds;

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(9374);
    server_len = sizeof(server_address);

    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

    listen(server_sockfd, BACK_LOG);

    FD_ZERO(&readfds);
    FD_SET(server_sockfd, &readfds);

    while(1)
    {
        char ch;
        int fd;
        int nread;
        testfds = readfds;

        cout<<"server waiting"<<endl;

        result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, 
                0);
        if(result < 1)
        {
            perror("select");
            exit(1);
        }
        for(fd = 0; fd < FD_SETSIZE; ++fd)
        {
            if(FD_ISSET(fd, &testfds))
            {
                if(fd == server_sockfd)
                {
                    client_len = sizeof(client_address);
                    client_sockfd = accept(server_sockfd,
                            (struct sockaddr *)&client_address,
                            (socklen_t*)&client_len);
                    FD_SET(client_sockfd, &readfds);
                    cout<<"adding client on fd:"<<client_sockfd<<endl;
                }
                else
                {
                    ioctl(fd, FIONREAD, &nread);
                    if(nread == 0)
                    {
                        close(fd);
                        FD_CLR(fd, &readfds);
                        cout<<"removing client on fd:"<<fd<<endl;
                    }
                    else
                    {
                        read(fd, &ch, 1);
                        sleep(1);
                        cout<<"serving client on fd:"<<fd<<endl;
                        ch++;
                        write(fd, &ch, 1);
                    }
                }
            }
        }
    }
    return 0;
}
